const goodsService = require('../service/goods.service')
const resultFormat = require('../middleware/resultFormat.Middleware')

class GoodsController {
    getGoodsList(req) {
        // select * from goods as g limit 10,10  从第几个开始 展示多少个
        let num = parseInt(req.query.num) //第几页
        let size = parseInt(req.query.size) //每页多少个
        //判断页码和数量限制
        if (isNaN(num) || isNaN(size)) num = 1, size = 20;
        //声明构造sql时用到的变量
        let id = req.query.id
        let data, sql, where, limit;
        limit = `limit ${num * size - size},${size}`
        where = id ? `where goods_id = ${id}` : ''

        sql = `select *
               from songshu.goods ${where} ${limit};`

        return goodsService.getGoodsList(sql)
    }
    getGoodsTotal(){
        return goodsService.getGoodsTotal()
    }
}

module.exports = goodsController = new GoodsController()
